const mixins = require("../mixins");
var app = getApp()
import Dialog from '../../../dist/dialog/dialog';
Page({
  behaviors: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    integral:'',
    active: 0,
    active1: 0,
    show: false,
    show1:false,
    contentRule:'',
    checked:false,
    imagelist: [],
    activity_list:[],
    listData:[],
    count:"",
    page:1,
    last_page:1,
    info:{},
    love_name:"",
    integral_name:"",
    within_time:false,
    point_name:"",
    width:0,
    currentTime:Date.parse(new Date())/1000,
    activity_id:"",//活动id
    stage_status:"", //活动状态id
    _arr:[],
    loading:false,
    integral_switch:false,//积分开关
    point_switch:false, //消费积分开关
    point_integral:false,
    integral_deduction:false,
    is_privilege:1,//是否优先消费认购
    integral_pay:0,//消费积分数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(",");
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          if (chil_arr[0] == "category_id") {
            // chil_arr[1] 就是category_id的值
            this.setData({
              category_id:chil_arr[1]
            })
        
          }
        }
      }
    }

    this.getDataCategory()

    const query = wx.createSelectorQuery();
    query.select('.subscribe').boundingClientRect( (res)=> {
      let width = res.width;
      this.setData({width})
    })
    query.exec();
  },

  link_poor(){
    wx.navigateTo({
      url: '/packageI/poor_reward/poor_reward',
    })
  },
  change_switch(e){
    let type = e.currentTarget.dataset.type;
    let switch_value = e.detail;
    if(this.data.integral_switch && this.data.point_switch){
      if(type == 'integral'  ){
        if(switch_value && this.data.point_integral){
          this.setData({
            integral_deduction:switch_value,
            point_integral:!switch_value
          })
        }else{
          this.setData({
            integral_deduction:switch_value,
            // point_integral:!switch_value
          })
        }
      }else if(type == 'points'){
        if(switch_value && this.data.integral_deduction){
          this.setData({
            integral_deduction:!switch_value,
            point_integral:switch_value
          })
        }else{
          this.setData({
            // integral_deduction:!switch_value,
            point_integral:switch_value
          })
        }
      }
      if(!this.data.point_integral && !this.data.integral_deduction){
        app.tips(`必须选择${this.data.point_name}或${this.data.integral_name}其中一种抵扣类型`)
      }
    }else{
      if(type == 'integral'){
        this.setData({integral_deduction:switch_value})
      }else if(type == 'points'){
        this.setData({point_integral:switch_value})
      }
    }
    this.get_ready()
  },
  onChangeTime(e) {
    this.data._arr[e.target.dataset.id] = {
      ...e.detail,
      id:e.target.dataset.id
    }
    this.setData({
      timeData: this.data._arr,
    });
  },
  link(e){
    let url = e.currentTarget.dataset.url;
    if(url){
      wx.navigateTo({
        url: url,
      })
    }
  },
  activeInfo({detail}){
    let stage_slot = detail.data.stage_slot || [],
    stage_list = detail.data.stage_list.data || [],
    activity_id = detail.activity_id,
    stage_status = detail.stage_status;
    this.setData({
      love_name:detail.data.love_name || '爱心值',
      point_name:detail.data.point_name || '积分',
      integral_name:detail.data.integral_name || '消费积分',
      page:detail.data.stage_list.current_page,
      last_page:detail.data.stage_list.last_page,
      active:detail.active,
      status_list:stage_slot,
      listData:stage_list,
      activity_id,
      stage_status
    })

  },
  getDataCategory(){
    app._postNetWork({
      url:app.getNetAddresss("plugin.subscription.frontend.modules.category.controllers.page.index"),
      data:{category_id:this.data.category_id},
      success:res=>{
        if(res.data.result){
          let data = res.data.data;
          this.setData({
            show1:!data.is_signed,
            activity_list : data.activity_list,
            imagelist: data.banner_list
          })
          if(!data.is_signed){
            this.getRule()
          }
        }else if(res.data.result==0){
          
          this.getData()
        }
      }
    })
  },
  getData(){
    app._postNetWork({
      url:app.getNetAddresss("plugin.subscription.frontend.modules.activity.controllers.page.index"),
      success:res=>{
        if(res.data.result){
          let data = res.data.data;
          this.setData({
            show1:!data.is_signed,
            activity_list : data.activity_list,
            imagelist: data.banner_list
          })
          if(!data.is_signed){
            this.getRule()
          }
        }else{
          app.tips(res.data.msg)
        }
      }
    })
  },
  //协议内容
  getRule(){
    app._postNetWork({
      url:app.getNetAddresss("plugin.subscription.frontend.modules.treaty.controllers.content.index"),
      success:res=>{
        if(res.data.result){
          this.setData({
            contentRule:res.data.data.content
          })
        }else{
          app.tips(res.data.msg)
        }
      }
    })
  },
  setShow() {
    this.setData({
      show:!this.data.show
    })
  },
  onChangePay(e){
    this.setData({count:e.detail.value})
    this.get_ready()
  },
  get_ready(){
    app._postNetWork({
      url:app.getNetAddresss('plugin.subscription.frontend.modules.stage.controllers.buy.ready'),
      data:{
        stage_id: this.data.info.id,
        quantity: this.data.count
      },
      success:res=>{
        if(res.data.result == 1){
          let { integral_pay, point_switch, integral_switch,is_privilege, love_pay, point_pay } = res.data.data;
          this.setData({
            point_switch:point_switch==0?false:true, 
            integral_switch:integral_switch==0?false:true,
            is_privilege,
            integral_pay,
            love_pay,
            point_pay
          })
          // if(!this.data.point_integral){
          //   this.setData({love_pay:this.data.count})
          // }else{
          //   this.setData({love_pay})
          // }
        }
      }
    })
  },
  onClose(){
    this.setData({
      show:false,
      count:'',
      love_pay:0,
      integral_pay:0,
      point_pay:0,
      point_integral:false,
      integral_deduction:false,
     })
  },
  confirm(){
    this.setData({loading:true})

    let json = {
      stage_id: this.data.info.id,
      quantity: this.data.count,
    }
    if(this.data.is_privilege == 2){
      if(this.data.point_integral){
        json.privilege_type = 'point';
      }else if(this.data.integral_deduction){
        json.privilege_type = 'integral';
      }else{
        json.privilege_type = '';
      }
    }
   
    app._postNetWork({
      url:app.getNetAddresss("plugin.subscription.frontend.modules.stage.controllers.buy.generate"),
      data:json,
      success:res=>{
        this.setData({loading:false})
        if(res.data.result){
          Dialog.alert({
            message: res.data.msg,
          }).then(() => {
            this.setData({
              show:false,
              count:'',
              love_pay:0,
              point_pay:0,
              integral_pay:0
            })
            this.getActiveData()
          });
        }else{
          Dialog.alert({
            message: res.data.msg,
          }).then(() => {});
        }
      }
    })
  },
  tosubscribe(e){
    let  data = e.currentTarget.dataset.info;
    app._postNetWork({
      url:app.getNetAddresss("plugin.subscription.frontend.modules.stage.controllers.buy.ready"),
      data:{
        stage_id: data.id,
        quantity: 1
      },
      success:res=>{
        if(res.data.result == 1){
          let { point_switch, integral_switch, is_privilege,integral_pay } = res.data.data;
         
          this.setData({
            point_switch:point_switch==0?false:true, 
            integral_switch:integral_switch==0?false:true,
            integral_pay,
            is_privilege,
            show:!this.data.show,
            info:data
          })
          // if(point_switch==1&&integral_switch==1){
          //   this.setData({point_integral:true,integral_deduction:false})
          // }else{
          //   if(point_switch==1){
          //     this.setData({point_integral:true})
          //   }else if(integral_switch == 1){
          //     this.setData({integral_deduction:true})
          //   }
          // }
        }
      }
    })
  },
  onChange(){
    this.setData({checked:!this.data.checked})
  },
  closeShow1(){
    if(!this.data.checked){
      return   wx.showToast({
        title: '请勾选协议',
        icon:"none"
      })
    }
    app._postNetWork({
      url:app.getNetAddresss("plugin.subscription.frontend.modules.treaty.controllers.agree.index"),
      success:res=>{
        if(res.data.result){
          app.tips(res.data.msg);
          this.setData({
            show1:false
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  getActiveData(){
    app._postNetWork({
      url:app.getNetAddresss("plugin.subscription.frontend.modules.activity.controllers.detail.index"),
      data:{
        activity_id:this.data.activity_id,
        stage_status:this.data.stage_status,
        page:1,
      },
      success:res=>{
        if(res.data.result){
          let data = res.data.data;
          let stage_slot =data.stage_slot || [],
          stage_list = data.stage_list.data || [];
          this.setData({
            page:data.stage_list.current_page,
            last_page:data.stage_list.last_page,
            status_list:stage_slot,
            listData:stage_list,
          })
        }else{
          app.tips(res.data.msg)
        }
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.page>=this.data.last_page){
      return false;
    }
    app._postNetWork({
      url:app.getNetAddresss("plugin.subscription.frontend.modules.activity.controllers.detail.index"),
      data:{
        activity_id:this.data.activity_id,
        stage_status:this.data.stage_status,
        page,
      },
      success:res=>{
        if(res.data.result){
          let data = res.data.data;
          this.setData({
            show1:!data.is_signed,
            activity_list : this.data.activity_list.concat(data.activity_list),
            imagelist: data.banner_list
          })
        }else{
          app.tips(res.data.msg)
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})